const router = require('koa-router')()

router

// 获取网站基本信息
.get('/getWebsiteInfo', async (ctx, next) => {
  try {
    let result = await ctx.db.WebsiteInfo.find()
    ctx.body = {
      code: 0,
      message: '查询成功',
      data: result[0]
    }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})

// 获取轮播图列表 ↓
.get('/getSlidesShowList', async (ctx,next) => {
  try {
    let res = await ctx.db.SlidesShow.find()
    ctx.body = { code: 0, message: '获取成功', data: res }
  } catch (error) {
    console.log(error.message)
    ctx.body = { code: 1, message: error.message, data: null }
  }
})

// 获取总数
.get('/getInfoNum', async (ctx, next) => {
  try {
    let articlesNum = await ctx.db.Articles.find().countDocuments()
    let ArticleTypesNum = await ctx.db.ArticleTypes.find().countDocuments()
    let GuestNum = await ctx.db.GuestBook.find().countDocuments()
    ctx.body = { code: 0, message: '总数获取成功', data: {
      articlesNum,
      ArticleTypesNum,
      GuestNum
    } }
  } catch (error) {
    console.log(error)
    ctx.body = { code: 1, message: error.message, data: null }
  }
}) 

module.exports = router